package good.threadlocalDemo;

public class ThreadLast implements Runnable{

	public static void main(String[] args) {
		//这个是可以用的
		ThreadLast ttll=new ThreadLast();
		/*int i=ttll.getNextNum();
		System.out.println(i);
		int j=ttll.getNextNum();
		System.out.println(j);*/
		Thread th1=new Thread(ttll,"one");
		Thread th2=new Thread(ttll,"two");
		Thread th3=new Thread(ttll,"three");
		th1.start();
		th2.start();
		th3.start();
	}
	private static ThreadLocal<Integer> tl =new ThreadLocal<Integer>(){
		public Integer initialValue(){
			return 0;
		}
	};
	//获取序列
	public int getNextNum(){
		tl.set(tl.get()+1);
		return tl.get();
	}
	private int g=1;
	public int getTestNum(){
		return g++;
	}
	//private int j=getNextNum();
	@Override
	public void run() {
		for(int i=0;i<100;i++){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			int j=getNextNum();
			int g=getTestNum();
			System.out.println(j+Thread.currentThread().getName()+g);
		}
	}

}
